package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.config.PicServerConfig;
import com.coocaa.internship.dao.CompanyDao;
import com.coocaa.internship.exception.BaseException;
import com.coocaa.internship.pojo.Company;
import com.coocaa.internship.service.CompanyService;
import com.coocaa.internship.util.FileNameUtil;
import com.coocaa.internship.util.FileSuffixUtil;
import com.coocaa.internship.util.FileUploadUtil;
import com.coocaa.internship.util.ParamvalidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description: 企业业务实习层
 * @Author: zwh
 * @Date: 2019-07-29
 * @Time: 22:01
 */
@Slf4j
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    CompanyDao companyDao;

    @Autowired
    PicServerConfig picServerConfig;

    @Override
    public ResultData insertCompany(MultipartFile logo, Company company) {
        ParamvalidatorUtil.paramValidate(company);
        String downloadPath = saveCompanyLogo(logo);
        if (downloadPath != null) {
            company.setCompanyLogo(downloadPath);
            Integer integer = companyDao.insertCompany(company);
            if (integer > 0) {
                return ResultData.success(null);
            }
        }
        return ResultData.failed();
    }

    @Override
    public ResultData deleteCompanyById(Long companyId) {
        ParamvalidatorUtil.paramValidate(companyId);
        Integer integer = companyDao.deleteCompanyById(companyId);
        if (integer > 0) {
            return ResultData.success(null);
        }
        return ResultData.failed();
    }

    @Override
    public ResultData selectCompanyById(Long companyId) {
        ParamvalidatorUtil.paramValidate(companyId);
        Company company = companyDao.selectCompanyById(companyId);
        if (company != null) {
            return ResultData.success(company);
        }
        return ResultData.success("未查询到企业信息");
    }

    @Override
    public ResultData selectCompaniesByName(String companyName) {
        ParamvalidatorUtil.paramValidate(companyName);
        List<Company> companies = companyDao.selectCompaniesByName(companyName);
        if (!companies.isEmpty()) {
            return ResultData.success(companies);
        }
        return ResultData.success("未查询到企业信息");
    }

    @Override
    public ResultData updateCompanyById(MultipartFile logo, Company company) {
        Company selectCompanyById = companyDao.selectCompanyById(company.getCompanyId());
        if (selectCompanyById != null) {
            String downloadPath = saveCompanyLogo(logo);
            if (downloadPath != null) {
                company.setCompanyLogo(downloadPath);
            }
            Integer integer = companyDao.updateCompanyById(company);
            if (integer > 0) {
                return ResultData.success(null);
            }
            return ResultData.failed();
        }
        return ResultData.builder()
                .code(ResultCode.COMPANY_NOT_EXISTS.getCode())
                .message(ResultCode.COMPANY_NOT_EXISTS.getMessage()).build();
    }

    @Override
    public String saveCompanyLogo(MultipartFile logo) {
        if (logo != null) {
            //原始文件名
            String originPicName = logo.getOriginalFilename();
            //文件后缀
            String suffix = originPicName.substring(originPicName.lastIndexOf(".") + 1);

            if (FileSuffixUtil.checkPicSuffix(suffix)) {
                //返回logo新名字
                String newPicName = FileNameUtil.getNewName(originPicName);
                //得到logo的真实路径
                String uploadPath = picServerConfig.getUploadPath() + "/" + newPicName;
                //存储logo
                boolean result = FileUploadUtil.upload(logo, uploadPath);
                if (result) {
                    //将简历的访问地址存入数据库
                    return picServerConfig.getDownloadPath() + uploadPath.substring(uploadPath.lastIndexOf("/"));
                }
            } else {
                throw new BaseException(ResultCode.FILE_SUFFIX_ILLEGAL.getCode()
                        , ResultCode.FILE_SUFFIX_ILLEGAL.getMessage());
            }
        }
        return null;
    }
}
